<template lang="pug">
el-dialog(title="创建订货单" :visible.sync="dialog" :before-close="close" append-to-body)
    goodslist(@getgoodsid="getgooditem")
    el-row
        el-col(:span="24")
            el-button(type="primary" @click="save") 保存
            el-button(type="primary" @click="open") 选择订货商品
        el-col.mgt(:span="24")
            el-col(:span="2")
                p 订货日期
            el-col(:span="10")
                el-date-picker(v-model="datetime" type="date" placeholder="选择日期(必填)")
    el-row
        el-col(:span="24")
            el-table(:data="td" style="width: 100%" height="350" v-loading="loading" element-loading-text="数据加载中...")
                el-table-column(prop="goodsId" label="商品编号")
                el-table-column(prop="name" label="商品名称")
                el-table-column(prop="spec" label="单位")
                el-table-column(label="数量")
                    template(slot-scope="scope")
                        el-input(type="number" v-model="scope.row.num" placeholder="必填")
                el-table-column(label="订货方")
                    template(slot-scope="scope")
                        el-select(v-model="scope.row.usernames" placeholder="订货方(必选)" :disabled="dinghuofang")
                            el-option(v-for="item in opt" :label="item.shopName" :key="item.shopId" :value="item.shopId")
                el-table-column(label="备注")
                    template(slot-scope="scope")
                        el-input(type="text" v-model="scope.row.remark" placeholder="选填")
                el-table-column(label="操作")
                    template(slot-scope="scope")
                        el-button(size="mini" type="danger" @click="del(scope.$index, scope.row)") 删除
</template>
<script>
    import moment from 'moment'
    import goodslist from 'components/GoodsLists'
    export default{
        data(){
            return{
                dinghuofang:!this.$store.state.shiro,
                dialog:false,
                datetime:'',
                td:[],
                loading:false,
                opt:[],
                shopID:-1
            }
        },
        components:{
            goodslist
        },
        computed:{
            getstate(){
                return this.$store.state.dinghuoclass;
            }
        },
        watch:{
            getstate(val){
                var _this=this
                axios.get('/shop/listShop.do').then(r=>{
                    if(r.data.code!=200){
                        _this.$message.error(r.data.message)
                        return
                    }
                    _this.opt=[].concat(r.data.response.shopList)
                })
                this.dialog=val
            }
        },
        mounted(){
            if(!this.$store.state.shiro){
                if(this.$store.state.TOPshopID){
                    this.shopID=this.$store.state.TOPshopID
                }
            }
        },
        methods:{
            chekcall(){//遍历所有填写的地方,效验
                if(!this.datetime){
                    this.$message.error('订货时间不能为空')
                    return false
                }
                for(var i=0;i<this.td.length;i++){
                    if(!this.td[i].num){
                        this.$message.error('数量不能为空')
                        return false
                    }
                    if(this.td[i].num<=0){
                        this.$message.error('数量必须大于0')
                        return false
                    }
                    if(!this.td[i].usernames){
                        this.$message.error('订货方不能为空')
                        return false
                    }
                }
                return true
            },
            checkacllback(arr){//遍历请求的返回值
                for(var i=0;i<arr.length;i++){
                    if(arr[i].data.code!=200){
                        this.$message.error(arr[i].data.message)
                        return false
                    }
                    return true
                }
            },
            save(){//保存
                var _this=this
                if(this.shopID==-1){
                    _this.$message.info('权限有误,不能添加')
                    return
                }
                if(!this.chekcall()){
                    return
                }
                this.$confirm('创建后无法修改,是否确定?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    var axiosList=new Array()
                    for(var i=0;i<_this.td.length;i++){
                        var pt=new URLSearchParams()
                        pt.append('goodId',_this.td[i].goodsId)
                        pt.append('goodName',_this.td[i].name)
                        pt.append('spec',_this.td[i].spec)
                        pt.append('num',_this.td[i].num)
                        pt.append('reserveTime',moment(_this.datetime).format("YYYY-MM-DD HH:mm:ss"))
                        if(_this.td[i].remark){
                            pt.append('remark',_this.td[i].remark)
                        }
                        pt.append('reserveShop',_this.shopID)
                        pt.append('shopId',_this.shopID)
                        axiosList.push(axios.get('/addReserveGoods.do',{params:pt}))
                    }

                    axios.all(axiosList).then(r=>{
                        if(_this.checkacllback(r)){
                            _this.$message.success('订货成功')
                            _this.$store.state.dinghuo=false
                            _this.close()
                        }else{
                            return
                        }
                    })

                }).catch(() => {
                    return 
                });
            },
            close(){//清空,关闭
                this.td=[]
                this.opt=[]
                this.datetime=''
                this.$store.state.dinghuoclass=false
            },
            open(){//打开商品选择
                this.$store.commit('ChangeWxGood')
            },
            getgooditem(m){//获取选择的商品,请求接口,获取商品详情
                var _this=this
                this.loading=true
                for(var i=0;i<m.length;i++){
                    var pp=new URLSearchParams()
                    pp.append('xcxGoodsId',m[i].goodsId)
                    axios.get('/getXcxGoodsById.do',{params:pp}).then(r=>{
                        // console.log('getXcxGoodsById',r)
                        if(r.data.code==200){
                            _this.td.push(r.data.response.xcxGoods)
                            if(!_this.$store.state.shiro){
                                for(var j=0;j<_this.td.length;j++){
                                    _this.td[j].usernames=_this.$store.state.TOPshopName
                                }
                            }
                        }else{
                            _this.$message.error(r.data.message)
                        }
                    })
                }
                _this.loading=false
            },
            del(i,r){//删除一行
                this.td.splice(i,1)
            }
        }
    }
</script>
<style scoped>
.mgt{
    margin-top: 20px;
}
</style>
